
import Base from './modules/base';
import GAME from 'common/const/game';
import BoxBase from './modules/boxes/boxBase';
import ajax from 'common/utils/ajax';
import generate from 'nanoid/generate';
import { isLocal } from 'common/utils';

/**
 * items
 */
import Needle from './modules/items/needle';
import Amethyst from './modules/items/amethyst';
import Glass from './modules/items/glass';
import Ink from './modules/items/ink';
import Poster from './modules/items/poster';
import SunBlock from './modules/items/sunBlock';

// award
import award_small_1 from 'common/asset/img/award/award_small_1.png';
import award_small_2 from 'common/asset/img/award/award_small_2.png';
import award_small_3 from 'common/asset/img/award/award_small_3.png';
import award_small_4 from 'common/asset/img/award/award_small_4.png';
import award_small_5 from 'common/asset/img/award/award_small_5.png';
import award_small_6 from 'common/asset/img/award/award_small_6.png';

import award_1 from 'common/asset/img/award/award_1.png';
import award_2 from 'common/asset/img/award/award_2.png';
import award_3 from 'common/asset/img/award/award_3.png';
import award_4 from 'common/asset/img/award/award_4.png';
import award_5 from 'common/asset/img/award/award_5.png';
import award_6 from 'common/asset/img/award/award_6.png';

class Store extends Base {

  boxes = [];

  items = [];

  constructor() {
    super();

    this.initBoxes();

    // 本地环境，等mockjs注入才发送请求
    if (isLocal) {
      setTimeout(() => {
        this.queryLottery();

        ajax.req('logPv', {});
        if (!localStorage.getItem('sxPlantGameUserToken')) ajax.req('logPv', {});
      }, 0);
    } else {
      this.queryLottery();

      ajax.req('logPv', {});
      if (!localStorage.getItem('sxPlantGameUserToken')) ajax.req('logPv', {});
    }


    this.uuid = this.getUuid;

  }

  initBoxes = () => {

    const { boxes } = GAME.background;
    let boxKeys = Object.keys(boxes);

    let boxModels = boxKeys.map(item => {
      return new BoxBase({
        key: item,
        parent: this,
        ...boxes[item],
      });
    });

    this.update('boxes', boxModels);

  };

  queryLottery = async () => {
    let data = await ajax.req('lottery.queryLotteryList', {});

    const award_small = {
      1: award_small_1,
      2: award_small_2,
      3: award_small_3,
      4: award_small_4,
      5: award_small_5,
      6: award_small_6,
    };
    const award_ = {
      1: award_1,
      2: award_2,
      3: award_3,
      4: award_4,
      5: award_5,
      6: award_6,
    };

    if (data && data.length > 0) {
      let list = data.map(item => {
        item.img = award_small[item.id];
        item.notice = award_[item.id];
        return item;
      });
      this.lotteryList = {
        list,
      };
    }

  };

  getItem = (items) => {

    items.forEach(item => {
      let goods = null;
      switch (item.key) {
        case 'needle': {
          goods = new Needle();
          break;
        }
        case 'amethyst': {
          goods = new Amethyst();
          break;
        }
        case 'glass': {
          goods = new Glass();
          break;
        }
        case 'ink': {
          goods = new Ink();
          break;
        }
        case 'poster': {
          goods = new Poster();
          break;
        }
        case 'sunBlock': {
          goods = new SunBlock();
          break;
        }
      }

      if (goods) this.items.push(goods);

      this.app.scene.itemFloor._renderItem();
    });

  };

  useItem = (key) => {

    const openKey = this.items.filter(n => n.key === key)[0];

    let index = this.items.indexOf(openKey);
    if (index > -1) {
      this.items.splice(index, 1);
    }

    this.app.scene.itemFloor._renderItem();
  };

  get getUuid() {

    let uuid = localStorage.getItem('sxPlantGameUserToken');
    if (!uuid) {
      uuid = `${Date.parse(new Date())}${generate('qwertyuiopasdfghjklzxcvbnm', 10)}`;
      localStorage.setItem(
        'sxPlantGameUserToken',
        uuid,
        {expires: '30d'}
      );
    }

    return uuid;

  }

}

export default new Store();
